/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦，本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_kit/zhiqim_git.htm
 *
 * ZhiqimGit is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.git.objer.model;

import org.zhiqim.git.GitConstants;
import org.zhiqim.git.objer.objectid.ObjectId;
import org.zhiqim.git.objer.objectid.RevCommit;

import org.zhiqim.kernel.util.Validates;

/**
 * 树条目模型对象
 *
 * @version v1.0.0 @author zouzhigang 2017-4-24 新建与整理
 */
public class TreeEntry implements GitConstants
{
    private Tree tree;          //所属树
    
    private int mode;           //模式
    private String name;        //名称
    private ObjectId oid;       //对象编号
    private RevCommit commit;   //关联提交
    
    public TreeEntry(Tree tree, int mode, String name, ObjectId oid)
    {
        this.tree = tree;
        this.mode = mode;
        this.name = name;
        this.oid = oid;
    }
    
    /******************************************************************************************/
    //基本属性
    /******************************************************************************************/
    
    public Tree getTree()
    {
        return tree;
    }
    
    public int getMode()
    {
        return mode;
    }
    
    public String getName()
    {
        return name;
    }
    
    public ObjectId getOid()
    {
        return oid;
    }
    
    /******************************************************************************************/
    //特殊属性
    /******************************************************************************************/
    
    public String getPath()
    {
        return (Validates.isEmpty(tree.getFolder())?"":(tree.getFolder()+"/")) + name;
    }
    
    public boolean isFile()
    {//标准文件或可执行文件
        return mode == MODE_FILE_REGULAR || mode == MODE_FILE_EXECUTABLE;
    }
    
    public boolean isFolder()
    {
        return mode == MODE_TREE;
    }
    
    /******************************************************************************************/
    //关联提交信息
    /******************************************************************************************/
    
    public TreeEntry setCommit(RevCommit commit)
    {
        this.commit = commit;
        return this;
    }
    
    public RevCommit getCommit()
    {
        return commit;
    }
    
    public boolean hasCommit()
    {
        return commit != null;
    }
    
    /******************************************************************************************/
    //toString
    /******************************************************************************************/
    
    @Override
    public String toString()
    {
        StringBuilder strb = new StringBuilder(isFolder()?"目录：":"文件：")
            .append(name).append("{"+mode+"}{").append(oid.getName()).append("}");
        
        if (commit != null)
            strb.append(commit.getCommitterIdent());
            
        return strb.toString();
    }
}
